﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.Linq;
using System.Text;

namespace mmoss.orm
{
    public partial class Cart
    {
        public Cart(Patient l_patient, Location l_location)
        {
            this.Patient = l_patient;
            this.Location = l_location;
            //add tax items
            //this.CartTaxs = new EntitySet<CartTax>();

        }
        public decimal CartCalculatedSubTotal
        {
            get
            {
                return CartItems.Sum(p => p.calculatedExtendedPrice);
            }
        }
        public decimal CartOriginalTotal
        {
            get
            {
                //calculate the total
                decimal dReturn = cartSubTotal;

                //calculate the taxes
                foreach (CartTax l_tax in CartTaxs)
                {
                    dReturn += Math.Round(l_tax.LocationTax.mmcTaxRate * cartSubTotal, 2);
                }
                return dReturn;
            }
        }
        public void calcCart()
        {
            //calculate the total
            cartSubTotal = CartItems.Sum(p => p.calculatedExtendedPrice);
            //cartSubTotal = CartCalculatedSubTotal
            cartTotal = cartSubTotal;
            decimal dDiscountedSubTotal = cartSubTotal;
            //subtract the discount
            if (cartDiscountDollar.HasValue)
            {
                dDiscountedSubTotal = cartSubTotal-cartDiscountDollar.Value;
            }
            cartTotal = dDiscountedSubTotal;
            //calculate the taxes
            
            foreach (CartTax l_tax in CartTaxs)
            {
                l_tax.amountRetained = Math.Round(l_tax.LocationTax.mmcTaxRate * dDiscountedSubTotal, 2);
                cartTotal+=l_tax.amountRetained;
            }
        }
    }
    public partial class CartTax
    {
        public string taxDesc
        {
            get { return this.LocationTax.mmcTaxDesc; }
        }
        public string taxRate
        {
            get { return Math.Round(this.LocationTax.mmcTaxRate * 100, 2) + " %"; }
        }
    }
    public partial class CartItem
    {
        public decimal calculatedExtendedPrice
        {
            get
            {
                return Math.Round(calculatedItemPrice * Quantity,2);
            }
        }
        public decimal calculatedItemPrice
        {
            get
            {
                InventoryItemByVendorMMC l_vendorItem = this.InventoryItemByVendorMMC;
                if (l_vendorItem.priceSingle.HasValue)
                {
                    //override price based on flat rate
                    return Math.Round(l_vendorItem.priceSingle.Value,2);
                }
                else
                {
                    //todo - fix to pull right values and offset
                    //find price from pricing table
                    return l_vendorItem.InventoryPriceTable.InventoryPriceTableItems.Single(
                        p => p.inventoryQuantTypeID == 1).nonMemberPrice;

                }
            }
        }
        public string displayName
        {
            get { return this.InventoryItemByVendorMMC.InventoryItem.itemShortDesc; }
        }
    }
    public static class ExtensionCart
    {
        public static List<Cart> GetOpenCartByPatientID(this Table<Cart> table
            , long l_patientID
            )
        {
            return (from objs in table
                        where (objs.patientID==l_patientID
                        && objs.cartCheckoutDate==null)
                        select objs).ToList();
        }
    }
}